<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ShenheAction;
use App\Models\Area;
use App\Models\City;
use App\Models\Factory;
use App\Models\RentPrice;
use App\Models\Tag;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class Factory3Controller extends AdminController
{
    protected $title = '转铺管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Factory::with(['tag', 'rentType', 'rentNature','city','peitao','town']), function (Grid $grid) {
            $grid->model()->where('type', 3)->orderBy('sort','desc')->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('rentType.name', '商
铺类型');
       //     $grid->column('rentNature.name', '商铺性质');
            $grid->column('city.name', '城市');
            $grid->column('area', '建筑面积㎡');
            $grid->column('total_price', '转让费')->display(function ($value) {
                return $value . '元';
            });
            $titles = ['id' => 'ID', 'title' => '标题', 'rentType' => '转铺类型','rentNature'=>'转铺性质','tag'=>'标签','peitao'=>'配套设施',
                'jingying'=>'经营状态','louceng'=>'楼层信息','cenggao'=>'层高（米）','jinshen'=>'进深（米）','kaijian'=>'开间(米)','area'=>'建筑面积',
               'total_price'=>'转让费(元)','city'=>'城市','town'=>'区域','address'=>'地址',
                'contact_name'=>'业主联系人',  'contact_mobile'=>'业主电话','broker_name'=>'经纪人姓名','broker_mobile'=>'经纪人电话',
            ];
            $grid->export()->xlsx()->titles($titles)->filename('铺源出售')->rows(function ($rows){
                foreach ($rows as $index => &$row) {
                    $row['rentType'] = $row['rent_type']['name'] ?? '';
                    $row['rentNature'] = $row['rent_nature']['name'] ?? '';
                    $row['tag'] = isset($row['tag'])&&$row['tag'] ? collect($row['tag'])->implode('name',',') :'';
                    $row['peitao']=isset($row['peitao'])&&$row['peitao'] ? collect($row['peitao'])->implode('name',',') : '';
                    $row['jingying']=Factory::$statusMap1[$row['status']] ?? '';
                    $row['louceng']=Factory::$louceng[$row['floor']] ?? '';
                    $row['city']=$row['city']['name'] ?? '';
                    $row['town']=$row['town']['name'] ?? '';
                }
                return $rows;
            });

            $grid->column('shenhe','审核状态')->using(Factory::$shenhe)->badge(
                [
                    0=>'danger',
                    1=>'success',
                    2=>'warning'
                ]
            );
            $grid->batchActions([
                new ShenheAction('审核通过',1),
                new ShenheAction('审核不通过',2),
            ]);
            $grid->column('tag', '标签')->display(function ($value) {
                return $value->pluck('name')->toArray();
            })->explode()->label();
            $grid->column('logo', '缩略图')->image('', 100, 100);
            $grid->column('is_display', '是否上架')->switch();
            $grid->column('is_hot', '是否热铺')->switch();
            $grid->column('is_jingxuan', '是否精选')->switch();
            $grid->column('sort','排序')->editable(true);
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title', '标题');
                $filter->equal('shenhe','审核状态')->select(Factory::$shenhe);
                $filter->equal('rent_type_id', '商铺类型')->select(\App\Models\FactoryRentType::where('type', 3)->pluck('name', 'id'));
              //  $filter->equal('rent_nature_id', '商铺性质')->select(\App\Models\FactoryRentNature::where('type', 3)->pluck('name', 'id'));
                $filter->equal('city_id', '城市')->select(City::all()->pluck('name', 'id'));
                $filter->equal('is_display', '是否上架')->select([0 => '下架', 1 => '上架']);
                $filter->equal('is_hot', '是否热铺')->select([0 => '否', 1 => '是']);
                $filter->equal('is_jingxuan', '是否精选')->select([0 => '否', 1 => '是']);
                $filter->where('area', function ($query) {
                    $area = Area::find($this->input);
                    return $query->where('area', '>=', $area->min)->where('area', '<=', $area->max);
                },'面积')->select(\App\Models\Area::where('type', 1)->pluck('name', 'id'));
                $filter->where('price', function ($query) {
                    $area = RentPrice::find($this->input);
                    return $query->where('price', '>=', $area->min)->where('price', '<=', $area->max);
                },'租金')->select(\App\Models\RentPrice::where('type', 1)->pluck('name', 'id'));
                $filter->where('tag', function ($query) {
                    return $query->whereHas('tag', function ($query) {
                        $query->whereIn('id', $this->input);
                    });
                }, '标签')->multipleSelect(Tag::all()->pluck('name', 'id'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Factory(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('lat');
            $show->field('lng');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make((Factory::with(['tag', 'rentType', 'rentNature'])), function (Form $form) {
            $form->text('title')->required();
            $form->select('rent_type_id', '商铺类型')->options(\App\Models\FactoryRentType::where('type', 3)->pluck('name', 'id'))->required();
          //  $form->select('rent_nature_id', '商铺性质')->options(\App\Models\FactoryRentNature::where('type', 3)->pluck('name', 'id'))->required();

            $form->switch('is_display', '是否上架')->options([0 => '下架', 1 => '上架']);
            $form->switch('is_hot', '是否是热铺')->options([0 => '否', 1 => '是']);
            $form->switch('is_jingxuan', '是否是精选')->options([0 => '否', 1 => '是']);
            $form->hidden('type')->default(3);
            $form->multipleSelect('tag', '标签')->options(Tag::all()->pluck('name', 'id'))->customFormat(function ($v) {
                if (!$v) {
                    return [];
                }
                // 从数据库中查出的二维数组中转化成ID
                return array_column($v, 'id');
            });
            $form->number('area', '建筑面积')->required();

            $form->select('floor','楼层信息')->options(Factory::$louceng);
            $form->select('status','经营状态')->options(Factory::$statusMap3);
            $form->number('cenggao','层高')->help('米');
            $form->number('jinshen','进深')->help('米');
            $form->number('kaijian','开间')->help('米');
            $form->number('total_price', '转让费')->help('元')->required();
        //    $form->number('price', '价格')->help('元/㎡');
            $form->image('logo', '首图')->chunkSize(1024)->autoUpload();
            $form->multipleImage('images', '轮播图')->removable()->chunkSize(1024)->autoUpload()->sortable();
            $form->select('city_id', '城市')
                ->options(\App\Models\City::all()->pluck('name', 'id'))
                ->load('town_id', '/api/town')
                ->required();
            $form->select('town_id','街区')->required();
            $form->text('address', '地址')->required();
            $form->map('baidu_lat', 'baidu_lng', '经纬度')->required();
            $form->editor('content', '详情');
            $form->text('contact_mobile', '业主电话');
            $form->text('contact_name', '业主联系人');
            $form->text('broker_mobile', '经纪人电话');
            $form->text('broker_name', '经纪人姓名');
            $form->text('sort','排序')->help('数字越大越靠前');
        });
    }
}
